package com.itheima.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Keyword;
import com.itheima.service.KeywordService;
import com.itheima.service.impl.KeywrodServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@WebServlet("/KeywordServlet")
public class KeywordServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String keyword = request.getParameter("keyword");
        KeywordService keywordService = new KeywrodServiceImpl();
        List<Keyword> keywords = keywordService.find(keyword);

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print(JSON.toJSONString(keywords));
    }
}